package com.gree.flowable.modeler.controller;

import com.gree.framework.entity.SysUser;
import com.gree.framework.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.flowable.idm.api.User;
import org.flowable.ui.common.model.RemoteUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

import static com.gree.flowable.modeler.tools.constants.FlowableConstants.FLOW_ABLE_MODELER_ROLES;

@Api(tags = "流程设计器用户权限配置")
@RestController
public class RemoteAccountResource {

    @ApiOperation("该接口时程设计器中 获取用户信息的相关接口 使用了spring security")
    @GetMapping("app/rest/account")
    public User getAccount() {
        String clientId = SecurityUtils.getClientId();
        RemoteUser sourceUser = new RemoteUser();
        SysUser loginUser = SecurityUtils.getUser().getUser();
        if ("gscm_client".equals(clientId)){
            sourceUser.setFirstName(loginUser.getNama());
            sourceUser.setDisplayName(loginUser.getNama());
            sourceUser.setPassword(loginUser.getPawd());
            sourceUser.setId(loginUser.getUsid());
        }else {
            String systemName = SecurityUtils.getUser().getUsername();
            sourceUser.setFirstName(systemName);
            sourceUser.setDisplayName("null");
            sourceUser.setPassword("123456");
            sourceUser.setId(systemName);
            sourceUser.setTenantId(systemName);
        }

        sourceUser.setPrivileges(new ArrayList<>(FLOW_ABLE_MODELER_ROLES));
        org.flowable.ui.common.security.SecurityUtils.assumeUser(sourceUser);
        return sourceUser;
    }


}